package models

import (
	"github.com/astaxie/beego/orm"
	//"github.com/astaxie/beego"
	"strconv"
)

//import "github.com/astaxie/beego/orm"

type PqTestPointCategory struct{
	CategoryNode
	Category
	LibType int
}
func (this *PqTestPointCategory) ListAll(q []interface{},code string) ([]orm.Params,interface{}) {
	var root PqTestPointCategory
	var nodes []orm.Params
	model := GetSlaveDb()
	_ = model.QueryTable("pq_test_point_category").Filter("code", code).One(&root)
	q = append(q, root.Lft,root.Rgt)
	_, _ = model.Raw(`select SQL_CALC_FOUND_ROWS *,'23/220' as finish_rate,4 as finish_star from pq_test_point_category where state > ? and lft >= ? and rgt <= ? order by level asc,ordering desc`, q).Values(&nodes)
	var maps []orm.Params
	//o := GetSlaveDb()
	_, _ = model.Raw("select count(*) as amount from pq_test_category where state > -1").Values(&maps)
	return nodes,maps[0]["amount"]
}
func (this *PqTestPointCategory) ReGenerateTestCategory(){
	slave := GetSlaveDb()
	master:= GetMasterDb()
	var root PqTestPointCategory
	slave.QueryTable("pq_test_point_category").Filter("id",1).One(&root)
	root.Lft = 1
	root.Rgt = 2
	master.Update(&root)
	this.ReGenerateChilds(root.Id)
}

func (this PqTestPointCategory) ReGenerateChilds(pid interface{})  {
	var nodes []orm.Params
	slave := GetSlaveDb()
	slave.Raw("select * from pq_test_point_category where pid=? and state > -1",pid).Values(&nodes)
	for _,v := range nodes{
		this.ReGenerateNode(v["id"])
		this.ReGenerateChilds(v["id"])
	}
}

func (this PqTestPointCategory)ReGenerateNode(id interface{}){
	slave := GetSlaveDb()
	master:= GetMasterDb()
	var node PqTestPointCategory
	var parent PqTestPointCategory
	slave.QueryTable("pq_test_point_category").Filter("id",id).One(&node)
	slave.QueryTable("pq_test_point_category").Filter("id",node.Pid).One(&parent)
	var nodes []orm.Params
	slave.Raw("select * from pq_test_point_category where rgt > ? and state > -1",parent.Rgt).Values(&nodes)
	//beego.Warning(nodes)
	for _,v := range nodes{
		//beego.Warning(nodes)
		rgt,_ := strconv.Atoi(v["rgt"].(string))
		lft,_ := strconv.Atoi(v["lft"].(string))
		level,_ := strconv.Atoi(v["level"].(string))
		up := orm.Params{
			"rgt" : rgt + 2,
		}
		if level >= parent.Level {
			up["lft"] = lft + 2
		}
		//beego.Warning(up)
		master.QueryTable("pq_test_point_category").Filter("id",v["id"]).Update(up)
	}
	node.Lft = parent.Rgt
	node.Rgt = parent.Rgt + 1
	//this.GetRealPath(node.Id)
	master.Update(&node)

	parent.Rgt = parent.Rgt + 2
	master.Update(&parent)
}


func (this PqTestPointCategory) GetRealPath(id int){
	var node PqTestPointCategory
	var nodes []orm.Params
	slave := GetSlaveDb()
    master := GetMasterDb()
	slave.QueryTable("pq_test_point_category").Filter("id",id).One(&node)
	slave.QueryTable("pq_test_point_category").Filter("lft__lt",node.Lft).Filter("rgt__gt",node.Rgt).Filter("level__gt",1).OrderBy("level").Values(&nodes)
	path := ""
	for k,n := range nodes{
		if k == 0{
			path = n["Title"].(string)
		}else{
			path = path + " > " + (n["Title"].(string))
		}


	}
	node.Path = path
	master.Update(&node)
}